/**
 * Copyright 2010 Zhou Zhao
 *
 * This file is part of FPGA compiler for EE680 USC
 *
   FPGA compiler is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FPGA compiler is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * The code in the class of XYLayout is used for layout the via and
 * wire according to their x and y coordinates in Java Swing framework.
 */

package edu.usc.ee.java;

import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.graph.Graph;
import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;
import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.map.LazyMap;

/**
 *
 * @author Zhou Zhao
 * @version 1.0
 */

public class XYLayoutBox extends AbstractLayout<via, wire>
{
    private List<via> vertex_list;
    
    Map<via, vertexData> vertexMap = LazyMap.decorate(
            new HashMap<via, vertexData>(),
            new Factory<vertexData>()
                {
                    public vertexData create()
                    {
                    return new vertexData();
                    }
                });

    public XYLayoutBox(Graph<via, wire> g)
    {
        super(g);
    }

    public void setVertex(List<via> list)
    {
        this.vertex_list = list;
    }

    public void reset()
    {
        initialize();
    }

    public void initialize()
    {
        Dimension d = getSize();

        if (d != null)
        {
            if(vertex_list == null)
            {
                setVertex(new ArrayList<via>(getGraph().getVertices()));
            }

            double height = d.getHeight();
            double width = d.getWidth();
/*
            int i = 0;
            for(via v : vertex_list)
            {
                    Point2D coord = transform(v);
                    coord.setLocation(i,i);
                    
                    vertexData data = getData(v);
                    data.setAngle(0);
                    i=i+100;
            }
*/

            for(ListIterator<via> it = vertex_list.listIterator(); it.hasNext();)
            {
                via sbox = it.next();
                
                Point2D coord = transform(sbox);
                coord.setLocation(sbox.getx()*100+50, sbox.gety()*100+50);

                vertexData data = getData(sbox);
                data.setAngle(0);
            }
            
        }
    }

    protected vertexData getData(via v)
    {
        return vertexMap.get(v);
    }

    protected class vertexData
    {
        private double angle;

/*        public vertexData()
        {
            
        }
*/
        protected double getAngle()
        {
            return angle;
        }

        protected void setAngle(double angle)
        {
            this.angle = angle;
        }

        @Override
        public String toString()
        {
            return "angle = " + angle;
        }
    }

}